package com.weiiew.common.config;

import com.weiiew.common.utils.ApiResponse;
import com.weiiew.common.exception.GatewayAccessDeniedException;
import com.weiiew.common.exception.OnlyFeignException;
import com.weiiew.common.exception.LockItemException;
import com.weiiew.common.exception.messageException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.dao.DuplicateKeyException;

import javax.annotation.PostConstruct;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/7/31
 */
@RestControllerAdvice
@Slf4j
@Order()
public class GlobalExceptionController {
    // 验证处理器加载
    @PostConstruct
    public void init() {
        log.info("全局异常处理器初始化完成");
    }

    @ExceptionHandler(value = DuplicateKeyException.class)
    public ApiResponse dealDuplicateKey(DuplicateKeyException e) {
        return ApiResponse.failure("插入值重复");
    }

    @ExceptionHandler(value = IllegalStateException.class)
    public ApiResponse dealIllegalStateException(IllegalStateException e){
        return ApiResponse.failure(e.getMessage());
    }
    @ExceptionHandler(value = messageException.class)
    public ApiResponse dealIllegalStateException(messageException e){
        return ApiResponse.failure(e.getMessage());
    }
    @ExceptionHandler(value = LockItemException.class)
    public ApiResponse dealIllegalStateException(LockItemException e){
        return ApiResponse.failure(e.getMessage());
    }

    @ExceptionHandler(value = GatewayAccessDeniedException.class)
    public ApiResponse dealAccessDeniedException(GatewayAccessDeniedException e){
        return ApiResponse.failure("当前网关无权访问此接口");
    }
    @ExceptionHandler(value = OnlyFeignException.class)
    public ApiResponse dealOnlyFeignException(OnlyFeignException e){
        return ApiResponse.failure(e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public ApiResponse dealException(Exception e) {
        e.printStackTrace();
        return ApiResponse.failure("Exception异常报错！！！");
    }
}
